package com.eternal.base.utils;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import com.eternal.framework.bus.RxBus;
import com.eternal.framework.utils.Utils;

/**
 * Created by Administrator
 * On 2020/4/28
 * Description:
 * @author Administrator
 */
public class TimeReceiver extends BroadcastReceiver {
    private static TimeReceiver receiver;

    private TimeReceiver() {}

    public static void register(){
        if(receiver==null){
            receiver=new TimeReceiver();
        }
        IntentFilter filter=new IntentFilter();
        filter.addAction(Intent.ACTION_TIME_TICK);
        Utils.getContext().registerReceiver(receiver, filter);
    }

    public static void unregister(){
        if(receiver!=null){
            try{
                Utils.getContext().unregisterReceiver(receiver);
            }catch (Exception e){
                e.printStackTrace();
            }
            receiver=null;
        }
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if(Intent.ACTION_TIME_TICK.equals(intent.getAction())){
            RxBus.getDefault().post(System.currentTimeMillis());
        }
    }
}
